package com.china08.yxyapi.api.manage.stu.mystudy.homework.dowork;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues.Article;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;

/**
 * 家长端作业系统 作业详情
 * 
 * @author yangyunying
 *
 */
@CrossOrigin
@RestController
public class DoWorkFindController extends YxyApiBaseController {

    @Autowired
    private HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Autowired
    private HomeworkTaskQuesRepository htr;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/homework/dowork/task/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel,
            RespModel respModel) {
        HomeworkTaskReport homeworktask = homeworkTaskReportRepository.findByStudentIdAndTaskId(reqModel.studentId,
                reqModel.taskId);
        if (homeworktask.getStatusOfFinished() == 0) {
            homeworktask.setStatusOfFinished(3);// 修改答题的状态为答题中
        }
        homeworktask = homeworkTaskReportRepository.save(homeworktask);
        respModel = new RespModel();
        respModel.title = homeworktask.getTitle();
        respModel.subTitle = homeworktask.getSubTitle();
        HomeworkTaskQues homeworkTaskQues = htr.findByTaskIdAndQuesType(reqModel.taskId, "2").get(0);
        if (homeworkTaskQues != null) {
            respModel.quesContent = homeworkTaskQues.getQuesContent();
            respModel.quesImage = homeworkTaskQues.getQuesImage();
            respModel.quesArticle = homeworkTaskQues.getQuesArticle();
        } else {
            respModel.quesContent = "";
            respModel.quesImage = new ArrayList<String>();
            respModel.quesArticle = new ArrayList<Article>();
        }
        // 获取通知类试题得状态值
        respModel.notiOfFinished = homeworktask.getNotiOfFinished();
        respModel.quesOfFinished = homeworktask.getQuesOfFinished();
        return respModel;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        /**
         * 学生id
         */
        public String studentId;

        /**
         * 任务id
         */
        public String taskId;

        @Override
        public void autoCheck() {
            Assert.isTrue(!"".equals(studentId) && studentId != null, "学生id不能为空");
            Assert.isTrue(!"".equals(taskId) && taskId != null, "任务id不能为空");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
        // 标题：通知标题 老师填写的标题
        public String title;
        // 副标题:试题标题
        public String subTitle;
        // 文本内容
        public String quesContent;
        // 通知类作业，上传的图片Url
        public List<String> quesImage;
        // 通知类作业，上传的文档,需要存储文档地址，文档的名称
        public List<Article> quesArticle;
        // 通知类试题状态值
        public Integer notiOfFinished;
        // 试题类状态值
        public Integer quesOfFinished;
    }
}
